package com.wwwarehouse.resource_center.fragment.createobject.creategoods;

import android.os.Bundle;
import android.os.Parcelable;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.resource_center.R;
import com.wwwarehouse.resource_center.bean.goods.SelectionUnitBean;
import com.wwwarehouse.resource_center.constant.ResourceConstant;
import com.wwwarehouse.resource_center.eventbus_event.RefreshEvent;
import com.wwwarehouse.resource_center.fragment.base.MyBaseViewPagerFragment;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Description:
 * Copyright  : Copyright (c) 2017
 * Author     : yfwang
 * Date       : 2017/10/19 20:11
 */
public class ChooseUnitFragment extends MyBaseViewPagerFragment {

    private int mNum = 24;
    public static final int GET_UNITS = 1;  //
    private String ownerUkid;
    private String unitUkid;
    private boolean barShow;

    @Override
    public void setTitle() {
        if (peekFragment() instanceof ChooseUnitFragment)
            mActivity.setTitle(getString(R.string.choose_unit));
    }

    @Override
    public void requestDatas() {
        barShow = isNavigationBarShow();
        if (barShow) {
            mNum = 20;
        }
        EventBus.getDefault().register(this);
        Bundle arguments = getArguments();
        if (null != arguments) {
            ownerUkid = arguments.getString("ownerUkid");
            unitUkid = arguments.getString("unitUkid");
        }
        httpPost(ResourceConstant.GET_UNITS, null, GET_UNITS, false, "");
    }

    @Override
    public void onSuccess(CommonClass commonClass, int what) {
        if (what == GET_UNITS) {
            List<SelectionUnitBean.EmptyIdentifierBean> unitBean = JSON.parseArray(commonClass.getData().toString(), SelectionUnitBean.EmptyIdentifierBean.class);
            Bundle bundle = new Bundle();
            bundle.putString("ownerUkid", ownerUkid);
            bundle.putString("unitUkid", unitUkid);
            bundle.putParcelableArrayList("data", (ArrayList<? extends Parcelable>) unitBean);
            SetFragmentAndPage(unitBean.size(), mNum, new UnitFragment().getClass().getName(), bundle);
        }
    }

    @Override
    public void onReLoad(int code) {
        requestDatas();
    }

    public void onEventMainThread(Object event) {
        if (event instanceof RefreshEvent && "UnitRefresh".equals(((RefreshEvent) event).getMsg())) {
            requestDatas();
        }


    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
